import React from 'react'
// ===================================================================== Temp
import SearchForm from '#cpt/temp/SearchForm'
import Table from '#cpt/temp/Table'
import AddEditModal from '#cpt/temp/AddEditModal'
import Controls from '#cpt/temp/Controls'
import TableControls from '#cpt/temp/TableControls'
import Delete from '#cpt/temp/Delete'
import Enable from '#cpt/temp/Enable'
// ===================================================================== global declare
const { $fn, $Lazy } = window
// ===================================================================== Lazy
const Page = $Lazy.load(()=>import('#tp/page/page-container'))
const DragLabel = $Lazy.state(()=>import('./cpt/drag-label'))
// ===================================================================== component
class Index extends React.Component{
	state = {
		data:[],
		pag: {},
		selectedKeys:[],
		// 搜索表单列表
		forms : [
			{ label: '模板编号',		name: 'tpl_number' },
			{ label: '状态',			name: 'enabled', type:'select', data:[{name:'启用',value:'1'},{name:'禁用',value:'0'}] },
		],
		// 提交表单列表
		formData : [
			/* 2021-7-19 添加描述，模板代码；删除纸型、只显示当前打印机纸型和内容设置 --马婷 */
			{ label: '标签模板名称',		name: 'tag_name', 	full:true, required:true },
			{ label: '模板代码',		name: 'tpl_code', 		full:true,	afterText:'注：模板代码请联系IT得到模板代码',	},
			{ label: '描述',		name: 'description', 	full:true,	type:'textarea' },
		],
	}
	
	model = {}
	
	componentDidMount(){
		if(window.isInit){
			this.fetch()
		}
	}
	
	// paging
	fetch = () => $fn.fetch.call(this,'lis-tag-template/index')
	
	// 表单
	cols = [
		{ title: '模板编号', 	dataIndex: 'tpl_number', 		width:200 },
		{ title: '标签名称', 	dataIndex: 'tag_name', 			width:150 },
		{ title: '创建时间', 	dataIndex: 'created_at', 		...$fn.table.time },
		{ title: '状态', 		dataIndex: 'enabled', 	width:200, render: text => { return +text === 1 ? '启用' : '禁用'} },
		{ title: '模板代码', 	dataIndex: 'tpl_code', 		width:200 },
		{ title: '描述', 	dataIndex: 'description', 		width:200 },
		TableControls.controls.call(this, { width: 180 })
	]
	
	// 表格操作按钮
	table_controls = (text,{ uuid, enabled } ) => {
		return [
			TableControls.edit.call(this, 'lis-tag-template/info', { uuid }, { // 编辑
				load: data => {
					// this.labelRef.setValue([{left: "175px",top: "35px"}])
				}
			}),			
			this.onDel([ uuid ]),													// 删除
			+enabled === 0 && this.onEnabled(1, [ uuid ]),							// 启用
			+enabled === 1 && this.onEnabled(0, [ uuid ]),							// 禁用
		]
	}
	// 操作按钮
	controls = () => {
		return [
			Controls.add.call(this),			// 添加
			this.onDel(),		// 删除
			this.onEnabled(0),	// 禁用
			this.onEnabled(1),	// 启用
		]
	}
	// 删除
	onDel = uuid => Delete.call(this, 'lis-tag-template/delete', { uuid, isKeys: !uuid, onComplete:()=>$fn.dataClear() })
	
	// 禁用/启用
	onEnabled = (enabled, uuid) => Enable.call(this, enabled,  { uuid, on:'lis-tag-template/on', off:'lis-tag-template/off', isKeys: !uuid, onComplete:()=>$fn.dataClear() })
		
	render(){
		return (
			<>
				<Page title='标签模板' controls={this.controls()}>
					{/* 搜索 */}
					{ SearchForm.call(this) }
					{/* 表格 */}
					{ Table.call(this,{isInit:window.isInit}) }
				</Page>
				{ AddEditModal.call(this,{ 
					labelWidth:'s7', 
					width:'80%', 
					height:'90%',
					isFlex:true, 
					minWidth:650,
					api: 'lis-tag-template/add', 
					editApi:'lis-tag-template/edit',
					onComplete:({ param, data })=>{
						$fn.dataClear()
					},
					suffix: <DragLabel onRef={ref=> this.labelRef = ref} />
				}) }
			</>
		)
	}
}

export default Index